package me.firstapp.bts.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.bts.service.SectionJobService;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.system.JsonApiClient;
import me.firstapp.spi.bbs.SectionService;
import me.firstapp.spi.system.ApiClientService;

@Service
public class SectionJobServiceImpl implements SectionJobService {
	private static Logger logger = LoggerFactory.getLogger(SectionJobService.class);

	@Autowired
	private SectionService sectionService;

	@Autowired
	private ApiClientService apiClientService;

	@Override
	public void updateTopicAndReplyCounts() {
		long start = System.currentTimeMillis();
		logger.info("..............开始批处理板块更新............");
		ListObject<JsonApiClient> clientsJson = apiClientService.findAll();
		if (clientsJson != null) {
			List<JsonApiClient> clients = clientsJson.getItems();
			if (clients != null && !clients.isEmpty()) {
				for (JsonApiClient client : clients) {
					SingleObject<Object> result = sectionService.updateTopicAndReplyCounts(client.getId());
					if (StatusHouse.COMMON_STATUS_OK.getCode().equals(result.getCode())) {
						logger.info("更新板块话题量和回复量成功。");
						// 更新板块缓存
						sectionService.updateSectionCache();
					} else {
						logger.info("更新板块话题量和回复量失败。==》" + result.getMsg());
					}
				}
			}
		}
		logger.info("................结束批处理板块更新.............耗时===》" + (System.currentTimeMillis() - start));
	}

}
